package com.baohua.dao.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.baohua.dao.BaseDao;
import com.baohua.dao.StaffDao;
import com.baohua.model.Role;
import com.baohua.model.Staff;

/**
 * StaffDao类
 * @author czp
 */

@Repository("staffDao")
public class StaffDaoImpl extends BaseDao implements StaffDao {

	@SuppressWarnings("unchecked")
	public List<Staff> queryStaff(String sql, int page, int pageSize) {
		Query query = this.getSession().createQuery(sql)
				.setFirstResult(page).setMaxResults(pageSize);
		List<Staff> list = query.list();
		return list;
	}

	public Staff queryStaffByStaffNo(String staffNo) {
		Staff staff = (Staff)this.getSession().createQuery("from Staff where staffNo = ?")
		   .setString(0, staffNo).uniqueResult();
		return staff;
	}

	public Staff queryStaffByName(String name) {
		Staff staff = (Staff)this.getSession().createQuery("from Staff where staffName = ?")
				   .setString(0, name).uniqueResult();
		return staff;
	}

	public void delStaff(Staff staff) {
		this.getHibernateTemplate().delete(staff);
	}

	public void updateStaff(Staff staff) {
		this.getHibernateTemplate().update(staff);
	}

	public void addStaff(Staff staff) {
		this.getHibernateTemplate().save(staff);
	}

	@Override
	public List<Staff> queryByStaffNOs(String[] staffNOs) {
		String hql = "from Staff t where t.staffNO in (:staffNOs)";
		List<Staff> list = this.getSession().createQuery(hql).setParameterList("staffNOs", staffNOs).list();
		return list;
	}
	
	@Override
	public long countStaff(String sql) {
		long count = (Long)this.getSession().createQuery(sql).uniqueResult();
		return count;
	}

	@Override
	public Staff queryStaffByStaffId(int staffId) {
		Staff staff = (Staff)this.getHibernateTemplate().load(Staff.class, staffId);
		return staff;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Role> queryRolesByStaffId(int staffId) {
		List<Role> list = this.getSession().createQuery("select s.roles from Staff s where s.staffId = ?")
				.setInteger(0, staffId).list();
		return list;
	}


}
